{% load staticfiles %}
<script type="text/javascript" src="{% static 'jquery.cookie.js' %}"></script>
<script type='text/javascript'>
    (function ($) {
        $(document).ready(function () {
            $("select[name$=zone], select[name$=template]").change(function () {
                var row = $(this).closest("div.grp-tr");
                var container = row.length > 0 ? row : "body";
                var zone = $("select[name$=zone]", container).val();
                var service = $("select[name$=template]", container).val();
                if (!zone || !service) return;
                $.post("{% url 'admin:get_price' 0 0 %}".replace('/0/0', ['/', service, '/', zone].join('')),
                        {csrfmiddlewaretoken: $.cookie('csrftoken')},
                        function (result) {
                            $("input[name$=price]", container).val(result);
                        }
                );
            });
            $("select[name$=connection_type]").change(function () {
                var type = $(this).val();
                if (!type) return;
                var row = $(this).closest("div.grp-tr");
                var container = row.length > 0 ? row : "body";
                $.post("{% url 'admin:get_service_code' 0 %}".replace('/0/', ['/', type, '/'].join('')),
                        {csrfmiddlewaretoken: $.cookie('csrftoken')},
                        function (result) {
                            $.each(result, function (key, val) {
                                $("textarea[name$=code_" + key + "]", container).val(val);
                            });
                        });
            });
        });
    })(grp.jQuery);
</script>